//
//  CJExten+String.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-14.
//

import UIKit
import Foundation

public extension String {
    
    /// 转Class
    func cj_class() -> AnyClass? {
        let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        guard let Class = NSClassFromString(namespace.replacingOccurrences(of: " ", with: "_") + "." + self) else {
            return nil
        }
        return Class
    }
    
    /// 判断是否包含 ignoringCase 忽略大小写
    func cj_contains(sub: String, ignoringCase: Bool = false) -> Bool {
        var options: String.CompareOptions = []
        if ignoringCase {
            options = .caseInsensitive
        }
        if self.range(of: sub, options: options) != nil {
            return true
        }
        return false
    }
    
    /// 根据字符进行分隔
    func cj_separatedBy(char: CharacterSet) -> [String] {
        let value = self.components(separatedBy: char)
        return value
    }
    
}

public extension String {
    
    /// 取字符串
    func cj_sub(start: Int, end: Int) -> String {
        let sIndex = index(startIndex, offsetBy: start)
        let eIndex = index(startIndex, offsetBy: end)
        if end >= self.count {
            return String()
        }
        return String(self[sIndex..<eIndex])
    }
    
    /// 取字符串
    func cj_sub(loc: Int, len: Int) -> String {
        let start = loc
        let end = loc + len
        let value = cj_sub(start: start, end: end)
        return value
    }
    
    /// 取字符串
    func cj_sub(to index: Int) -> String {
        let value = cj_sub(start: 0, end: index)
        return value
    }
    
    /// 取字符串
    func cj_sub(from index: Int) -> String {
        let value = cj_sub(start: index, end: (self.count - 1))
        return value
    }
    
    /// 取字符串
    func cj_index(loc: Int) -> String {
        let value = cj_sub(loc: loc, len: 1)
        return value
    }
    
}

public extension String {
    
    /// 转Int
    var cj_int: Int {
        guard let value = Double(self) else { return 0 }
        return Int(value)
    }
    
    /// 转Int 
    /// 4舍5入
    var cj_intRound: Int {
        guard let value = Double(self) else { return 0 }
        return lround(value)
    }

    /// 转Int 
    /// 下取整
    var cj_intFloor: Int {
        guard let value = Double(self) else { return 0 }
        return Int(floor(value))
    }
    
    
    /// 转Int 
    /// 上取整
    var cj_intCeil: Int {
        guard let value = Double(self) else { return 0 }
        return Int(ceil(value))
    }
    
    
    /// 转Data 
    /// encoding 默认.utf8
    func cj_data(encoding: String.Encoding = .utf8) -> Data? {
        return data(using: encoding)
    }
    
    /// 转拼音
    func cj_pinyin(_ ton: Bool = false) -> String {
        let value = NSMutableString(string: self)
        CFStringTransform(value, nil, kCFStringTransformToLatin, false)
        if ton {
            CFStringTransform(value, nil, kCFStringTransformStripDiacritics, false)
        }
        return value as String
    }
    
    /// 解析JSON
    var cj_dict: Any? {
        return cj_data()?.cj_dict
    }
    
    /// ascii字符串
    func cj_asciiString() -> String {
        var value = ""
        for us in self.unicodeScalars {
            value.append(String(format: "%X", us.value))
        }
        return value
    }
    
    /// 复制
    func cj_copy() {
        UIPasteboard.general.string = self
    }
}

public extension String {
    
    /// 时间format字符串转Date
    func cj_date(format: String = "YYYY-MM-dd HH:mm:ss", timeZone: TimeZone?, locale: Locale?) -> Date {
        let dateFormat = DateFormatter()
        dateFormat.dateFormat = format
        if let timeZone = timeZone {
            dateFormat.timeZone = timeZone
        }
        if let locale = locale {
            dateFormat.locale = locale
        }
        let date = dateFormat.date(from: self) ?? Date()
        return date
    }
    
    /// 时间戳转Date
    var cj_stampToDate: Date {
        let inter = self.cj_int
        var interval = Double(inter)
        // 时间戳 10位 13位
        interval = String(inter).count == 10 ? interval : interval/1000
        let date = Date(timeIntervalSince1970: interval)
        return date
    }
    
    /// 时间戳转DateFormat字符串 
    /// format默认 "YYYY-MM-dd HH:mm:ss"
    func cj_dateString(format: String = "YYYY-MM-dd HH:mm:ss") -> String {
        let date = cj_stampToDate
        let dateFormat = DateFormatter()
        dateFormat.dateFormat = format
        let dateString = dateFormat.string(from: date)
        return dateString
    }
    
    /// 时间戳转时钟 00:00:00
    var cj_clock: String {
        let seconds = self.cj_intRound
        let hour = String(format: "%02ld", seconds/3600)
        let minute = String(format: "%02ld", (seconds%3600)/60)
        let second = String(format: "%02ld", seconds%60)
        let clock = hour + ":" + minute + ":" + second
        return clock
    }
    
    /// 时间戳转时钟 00:00/00:00:00
    var cj_clockFits: String {
        let seconds = self.cj_intRound
        if seconds < 3600 {
            let minute = String(format: "%02ld", (seconds%3600)/60)
            let second = String(format: "%02ld", seconds%60)
            let clock = minute + ":" + second
            return clock
        } else {
            return cj_clock
        }
    }
}

public extension String {
    
    /// 16进制字符串转Data
    var cj_hexData: Data {
        let hexString = replacingOccurrences(of: " ", with: "")
        var hexs = Array<UInt8>()
        for i in 0..<hexString.count where i % 2 == 0 {
            let byteString = hexString.cj_sub(start: i, end: i + 2)
            hexs.append(UInt8(byteString, radix: 16) ?? 0)
        }
        let data = Data(hexs)
        return data
    }
    
    /// 16进制字符串转10进制字符串
    var cj_hexToDecimal: String {
        guard let value = Int(self, radix: 16) else { return "" }
        return String(value, radix: 10)
    }
    
    /// 16进制字符串转2进制字符串
    var cj_hexToBinary: String {
        guard let value = Int(self, radix: 16) else { return "" }
        return String(value, radix: 2)
    }
    
    /// 10进制字符串转16进制字符串
    var cj_decimalToHex: String {
        guard let value = Int(self, radix: 10) else { return "" }
        return String(value, radix: 16, uppercase: true)
    }
    
    /// 10进制字符串转2进制字符串
    var cj_decimalToBinary: String {
        guard let value = Int(self, radix: 10) else { return "" }
        return String(value, radix: 2)
    }
    
    /// 2进制字符串转16进制字符串
    var cj_binaryToHex: String {
        guard let value = Int(self, radix: 2) else { return "" }
        return String(value, radix: 16, uppercase: true)
    }
    
    /// 2进制字符串转10进制字符串
    var cj_binaryToDecimal: String {
        guard let value = Int(self, radix: 2) else { return "" }
        return String(value, radix: 10)
    }
}
